#include <iostream>

#include <stdio.h>

extern "C"
{
#include "z9.h"
}

using namespace std;

z9_event_loop *loop;

void accept_cb(z9_acceptor *a, int32_t fd, z9_sockaddr *addr, void *ud, int32_t err) {
	printf("accept_cb\n");
	int i;

	printf("-----ipstr: %s\n", inet_ntoa( addr->in.sin_addr ) );

	printf("ip: %d   port: %d\n", addr->in.sin_addr, addr->in.sin_port);
	/*char ipstr[20];
	memset(ipstr, 0, sizeof(ipstr));
	inet_ntop(AF_INET, (void*)(addr->in.sin_addr), ipstr, 16);
	printf("ipstr: %s \n", ipstr);*/


	z9_stream_socket_option option = {
		.recv_buffer_size = 1024*16,
	};
	z9_stream_socket *s = z9_stream_socket_new(fd, &option);

}

void test_tcp_server() {
	signal(SIGPIPE,SIG_IGN);
	loop = z9_loop_new();
	if (!z9_listen_tcp_ip4(loop, "106.14.38.203", 19999, accept_cb, NULL)) {
		printf("server start error\n");
	} else {
		z9_loop_run(loop);
	}
	return;
}

int main(int argc, char *argv[])
{
	char sz[] = "Hello, World!";	//Hover mouse over "sz" while debugging to see its contents
	cout << sz << endl;	//<================= Put a breakpoint here

	test_tcp_server();

	return 0;
}